package rabbit

import (
	"fmt"
	"github.com/streadway/amqp"
	"qq_service/server/base/msg_queue/rabbit/lib"
	"time"
)

func SendMail2(msgs <-chan amqp.Delivery, c string) {

	for msg := range msgs {
		{
			fmt.Println("向我开炮", c)
			time.Sleep(time.Second)
		}

		fmt.Println(msg.DeliveryTag, string(msg.Body))
		msg.Ack(false)
	}
}

func RabbitMQInit() {
	mq := lib.NewMQ()

	mq.Consume(lib.QueueNewUser, "c2", "c2", SendMail2)

}

//func Send(msg amqp.Delivery) {
//
//	time.Sleep(time.Second * 1)
//
//	userID := string(msg.Body)
//
//	isfail := true
//
//	delay := msg.Headers["x-delay"]
//
//	//模拟消息消费失败
//	for isfail {
//
//		r := helper.SetNotify(userID, 5)
//		newDelay := int(delay.(int32)) * 2
//		//失败重复
//		if r > 0 {
//			err := myClient.SendDelayedMessage(lib.RouterKeyUserReg, lib.ExchangeUserDelayed, userID, newDelay)
//			if err != nil {
//				log.Println(err)
//			}
//			log.Println("userID: ", userID, "重试: ", newDelay)
//		} else {
//			log.Printf("用户:%s 达到了最大重试次数", userID)
//			isfail = false
//			msg.Reject(false) //丢弃
//			break
//		}
//	}
//
//	fmt.Println("收到消息 ID: ", msg.DeliveryTag)
//	msg.Ack(false)
//
//}

func SendMail(msgs <-chan amqp.Delivery, c string) {

	for msg := range msgs {

		//go Send(msg)
		fmt.Println("向我开炮", msg)

		//if c == "c1" {
		//	fmt.Println("模拟出错")
		//	//重新入列
		//	msg.Reject(true)
		//	continue
		//}

	}

}

var myClient *lib.MQ

func main2() {

	myClient = lib.NewMQ()

	//消费者限流
	err := myClient.Ch.Qos(2, 0, false)
	if err != nil {
		fmt.Println(err.Error())
	}

	myClient.Consume(lib.QueueNewUser, "c1", "c1", SendMail)

}
